import store from '@/store'

// 检查是否有权限，例子：v-if="hasRolePerm(['admin'])"
export function hasRolePerm(value) {
  if (value && value instanceof Array && value.length > 0) {
    const isAdmin = store.getters && store.getters.isAdmin
    if (isAdmin) return true

    const userRoles = store.getters && store.getters.roles
    const hasPerm = userRoles.some(role => {
      return value.includes(role)
    })
    return hasPerm
  } else {
    // 没有权限
    return false
  }
}

// 检查是否有权限，例子：v-if="hasActionPriv(['sys:user:query])"
export function hasActionPriv(value) {
  const isAdmin = store.getters && store.getters.isAdmin
  if (isAdmin) return true

  const privList = store.getters && store.getters.priv
  const hasPerm = privList.some(priv => {
    return value.includes(priv.privId)
  })
  return hasPerm
}
